<?php
/**
 * Log日志 用于项目的代码调试，区分debug模式开关及不同环境(dev/test/pro)
 * @todo 需要添加区分服务器环境的配置
 * @todo 后续建立了自己的log组件服务，要支持到网络提交
 * @author flwu
 *
 */
class Log {
	
	private static $_arrLogs = array();
	
	public static function debug($strMessage, $arrTags = array()) {
		if (__DEBUG != 'NONE') {
			$strHostName = gethostname();
			$arrTrace = debug_backtrace();
			$param = array(
				'level' => 'DEBUG',
				'message' => $strMessage,
				'tags' => implode('、', $arrTags),
				'host' => $strHostName,
				'file' => $arrTrace[0]['file'],
				'class' => $arrTrace[1]['class'],
				'function' => $arrTrace['1']['function'],
				'time' => date('H:i:s')
			);
			self::$_arrLogs[] = $param;
		}
	}
	
	public static function warning($strMessage, $arrTags = array()) {
		if (__DEBUG != 'NONE') {
			$strHostName = gethostname();
			$arrTrace = debug_backtrace();
			$param = array(
					'level' => 'WARNING',
					'message' => $strMessage,
					'tags' => implode('、', $arrTags),
					'host' => $strHostName,
					'file' => $arrTrace[0]['file'],
					'class' => $arrTrace[1]['class'],
					'function' => $arrTrace['1']['function'],
					'time' => date('H:i:s')
			);
			self::$_arrLogs[] = $param;
		}
	}
	
	public static function error($strMessage, $arrTags = array()) {
		if (__DEBUG != 'NONE') {
			$strHostName = gethostname();
			$arrTrace = debug_backtrace();
			$param = array(
					'level' => 'ERROR',
					'message' => $strMessage,
					'tags' => implode('、', $arrTags),
					'host' => $strHostName,
					'file' => $arrTrace[0]['file'],
					'class' => $arrTrace[1]['class'],
					'function' => $arrTrace['1']['function'],
					'time' => date('H:i:s')
			);
			self::$_arrLogs[] = $param;
		}
	}
	
	public static function fetal($strMessage, $arrTags = array()) {
		if (__DEBUG != 'NONE') {
			$strHostName = gethostname();
			$arrTrace = debug_backtrace();
			$param = array(
					'level' => 'FETAL',
					'message' => $strMessage,
					'tags' => implode('、', $arrTags),
					'host' => $strHostName,
					'file' => $arrTrace[0]['file'],
					'class' => $arrTrace[1]['class'],
					'function' => $arrTrace['1']['function'],
					'time' => date('H:i:s')
			);
			self::$_arrLogs[] = $param;
		}
	}
	
	public static function commit() {
		if (__DEBUG == 'FILE') {
			self::_displayFile();
		} elseif (__DEBUG == 'BROWER') {
			self::_displayBrower();
		} else {
				
		}
	}
	
	private static function _displayFile() {
		if (!empty(self::$_arrLogs)) {
			foreach (self::$_arrLogs as $log) {
				$strMessage = "LEVEL : " . $log['level'] . "\t" . "MESSAGE : " . $log['message'] . "\t" . $log['tags'] . "\t";
				$strMessage .= "HOST : " . $log['host'] . "\tFILE : " . $log['file'] . "\t" . "CLASS : " . $log['class'] . "\t" . "FUNCTION : " . $log['function'] . "\ttime : " . $log['time'];
				file_put_contents(_APP_ROOT . 'log/' . date('Y-m-d') . '.log', $strMessage . "\n", FILE_APPEND);
			}
		}
	}
	
	private static function _displayBrower() {
		if (!empty(self::$_arrLogs)) {
			echo "<style> body{font-size:9pt;}	td{font-size:9pt;} </style>";
			echo "<table border=\"0\" cellpadding=\"4\" cellspacing=\"1\" width=\"100%\">";
			echo "<tr bgcolor=\"#D2D2D2\"><td width=5%>序号</td><td width=15%>级别</td><td width=40%>内容</td> <td width=10%>主机</td><td width=30%>文件路径</td><td width=15%>类名</td><td width=15%>方法名</td><td width=15%>时间</td></tr>";
			foreach(self::$_arrLogs as $key => $log){
				$strTemp = "<tr bgcolor=\"E2E2E2\"><td>" . ($key + 1) . "</td>";
				switch ($log['level']) {
					case "DEBUG":
						$strTemp .= "<td align=left bgcolor='green'> " . $log['level'] . "</td> ";
						break;
					case "ERROR":
						$strTemp .= "<td align=left bgcolor='red'> " . $log['level'] . "</td> ";
						break;
					case "WARNING":
						$strTemp .= "<td align=left bgcolor='yellow'> " . $log['level'] . "</td> ";
						break;
					case "FETAL":
						$strTemp .= "<td align=left bgcolor='red'> " . $log['level'] . "</td> ";
						break;
				}
				$strTemp .= "<td align=left> " . $log['message'] . " </td><td align=left> " . $log['host'] . "</td><td align=left> " . $log['file'] . "</td><td align=left>" . $log['class'] . " </td><td align=left> " . $log['function'] . " </td><td align=left>" . $log['time'] . " </td></tr>";
				echo $strTemp;
			}
			echo "</table>";
		}
	}
}